package com.huitone.gddw.service.impl.worktable;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.SmSign;
import com.huitone.gddw.dao.worktable.paramconfigmanage.ISmSignDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.ISmSignService;
import com.huitone.gddw.service.dto.SmSignDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 短信签名管理 接口实现类
 * @author wuluodan
 *
 */
@Service
public class SmSignService implements ISmSignService {

	@Autowired
	private ISmSignDao smSignDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;
	
	@Override
	public QueryPage<SmSignDTO> selectPage(QueryPage<SmSign> page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("signPos", page.getParams().get("signPos"));
		map.put("srvNode", page.getParams().get("srvNode"));
		map.put("serviceCode", page.getParams().get("serviceCode"));
		map.put("status", page.getParams().get("status"));
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<SmSignDTO> result = smSignDao.selectPage(map);
		int totalCount = smSignDao.selectCount(map);
		
		QueryPage<SmSignDTO> queryPage = new QueryPage<SmSignDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public SmSign selectByPrimaryKey(String recId) {
		return smSignDao.selectByPrimaryKey(recId);
	}

	@Override
	public SmSignDTO toUpdate(String recId) {
		SmSign entity = this.selectByPrimaryKey(recId);
		SmSignDTO smSignDTO = new SmSignDTO(entity);
		return smSignDTO;
	}

	@Override
	public void changeStatus(SmSign entity) {
		this.update(entity);
	}

	@Override
	public void save(SmSign entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		smSignDao.insertSelective(entity);
		
		this.updateParamRec("保存短信签名");
	}

	@Override
	public void update(SmSign entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		smSignDao.updateByPrimaryKey(entity);
		
		this.updateParamRec("更新短信签名");
	}

	@Override
	public void delete(String recId) {
		smSignDao.deleteByPrimaryKey(recId);
		this.updateParamRec("删除短信签名");
	}

	@Override
	public List<SmSignDTO> getRecord(Map<String, Object> params) {
		List<SmSignDTO> list = smSignDao.getRecord(params);
		return list;
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.SM_SIGN, desc);
	}

}
